<template>
  <div class="antenna_container p-b-50px">
    <!-- 天线管理卡片 -->
    <el-card>
      <template #header>
        <div class="flex justify-between items-center">
          <div class="flex items-center">          
            <span class="font-bold text-16px">{{ t('antennaManage.antenna_manage_label') }}</span>
          </div>
          <!-- <el-button type="primary" @click="handleAddAntenna">
            <el-icon><Plus /></el-icon>
            {{t('antennaManage.add_antenna')}}
          </el-button> -->
        </div>
      </template>
      <el-table 
        :row-class-name="tableRowClassName" 
        :data="antennaList" 
        :row-key="(row: any) => row.id"
        :expand-row-keys="expands"
      >
        <el-table-column type="expand">
          <template v-slot="{ row: { id,param, status } }">
            <div class="p-20px">
              <!-- 参数设置卡片 -->
              <el-card shadow="never" class="m-b-20">
                <template #header>
                  <div class="flex justify-between items-center w-100%">
                    <div class="flex items-center">
                      <el-icon class="m-r-10px"><Setting /></el-icon>
                      <span>{{ t('antennaManage.param_setting') }}</span>
                    </div>
                    <div class="global_btn" @click="handleEditParam(param,id)">
                      <el-icon><Edit /></el-icon>
                       <span >{{t('antennaManage.edit_param_label')}}</span>
                    </div>
                  </div>
                </template>
                <el-form :model="status" label-suffix=":" class="status-form">
                  <el-form-item :label="t('antennaManage.transmit_switch_label')">
                    <TagRender :value="param.transmit_switch" name="antennaSwitch" />
                  </el-form-item>
                  <!-- <el-form-item :label="t('antennaManage.receive_switch_label')">
                    <TagRender :value="param.receive_switch" name="antennaSwitch" />
                  </el-form-item> -->
                  <el-form-item :label="t('antennaManage.track_switch_label')">
                    <TagRender :value="param.tracking_switch" name="antennaSwitch" />
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.transmit_frequency_label')">
                    <span>{{ param.transmit_frequency }}</span>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.receive_frequency_label')">
                    <span>{{ param.receive_frequency }}</span>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.transmit_polarization_label')">
                    <span>{{ setPolarization(param, "transmit") }}</span>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.receive_polarization_label')">
                    <span>{{ setPolarization(param, "receive") }}</span>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.buc_attenuation_label')">
                    <span>{{ param.buc }}</span>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.bdc_attenuation_label')">
                    <span>{{ param.bdc }}</span>
                  </el-form-item>
                </el-form>
              </el-card>
              <!-- 工作状态卡片 -->
              <el-card v-if="status" shadow="never">
                <template #header>
                  <div class="flex items-center">
                    <el-icon class="m-r-10px"><Monitor /></el-icon>
                    <span>{{ t('antennaManage.work_status_label') }}</span>
                  </div>
                </template>
                <el-form :model="status" label-suffix=":" class="status-form">
                  <el-form-item :label="t('antennaManage.type_label')">
                    <span>{{ status.type }}</span>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.work_mode_label')">
                    <TagRender :value="status.mode" name="workMode" />
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.work_status_label')">
                    <TagRender :value="status.status" name="antennaStatus" />  
                  </el-form-item>            
                  <el-form-item :label="t('antennaManage.buc_warn_label')">
                    <span>{{ status.buc_warn ==='normal'?"正常":"无"}}</span>  
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.bdc_warn_label')">
                    <span>{{ status.bdc_warn ==='normal'?"正常":"无"}}</span>    
                  </el-form-item>            
                  <el-form-item :label="t('antennaManage.temp_label')">
                    <span>{{ status.temp }}</span>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.elevate_label')">
                    <span>{{ status.elevate }}</span>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.azimuth_label')">
                    <span>{{ status.azimuth }}</span>
                  </el-form-item>
             <!--      <el-form-item :label="t('antennaManage.inertial_navigate_warn_label')">
                    <TagRender :value="status.inertial_navigate_warn" name="antennaStatus" />
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.over_stable_warn_label')">
                    <TagRender :value="status.overtemp_warn" name="antennaStatus" />
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.transmit_front_status_1_label')">
                    <TagRender :value="status.transmit_front_status_1"  name="antennaStatus" />
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.transmit_front_status_2_label')">
                    <TagRender :value="status.transmit_front_status_2"  name="antennaStatus" /> 
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.transmit_front_status_3_label')">
                    <TagRender :value="status.transmit_front_status_3"  name="antennaStatus" /> 
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.transmit_front_status_4_label')">
                    <TagRender :value="status.transmit_front_status_4"  name="antennaStatus" /> 
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.receive_front_status_1_label')">
                    <TagRender :value="status.receive_front_status_1"  name="antennaStatus" />
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.receive_front_status_2_label')">
                    <TagRender :value="status.receive_front_status_2"  name="antennaStatus" />
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.receive_front_status_3_label')">
                    <TagRender :value="status.receive_front_status_3"  name="antennaStatus" />
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.receive_front_status_4_label')">
                    <TagRender :value="status.receive_front_status_4"  name="antennaStatus" />
                  </el-form-item> -->
                </el-form>
              </el-card>
            </div>
          </template>
        </el-table-column>
        <el-table-column  :label="t('antennaManage.name')" prop="communication.name" />
        <el-table-column :label="t('antennaManage.ip_addr')" prop="communication.ip_addr" />
        <el-table-column :label="t('antennaManage.ant_port')" prop="communication.ip_port" />
        <el-table-column :label="t('antennaManage.local_port')" prop="communication.local_port" />
        <el-table-column :label="t('antennaManage.serial_device')" prop="communication.serial_name" />
        <el-table-column :label="t('antennaManage.cp_label')" prop="communication.cp_device" />
        <el-table-column :label="t('antennaManage.enable')" prop="communication.enable">
          <template v-slot="{ row }">
            <span>{{ row.communication.enable==="on" ? t('antennaManage.enable_true') : t('antennaManage.enable_false') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="t('antennaManage.status')" prop="communication.status">
          <template v-slot="{ row }">
            <span>{{ row.communication.status==="on" ? t('antennaManage.status_true') : t('antennaManage.status_false') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="t('antennaManage.operation.label')" min-width="120px" >
          <template v-slot="{ row }">
            <div class="flex  items-center gap-10px " >
              <div class="global_btn" @click="expandChange(row)" >
                <el-icon>
                  <ArrowDown v-if="!expands.includes(row.id)" />
                  <ArrowUp v-if="expands.includes(row.id)" />
                </el-icon>
                <span class="m-l-5px table_btn">
                  {{ !expands.includes(row.id) ? t('antennaManage.operation.expand'): t('antennaManage.operation.fold')}}
                </span>
              </div>
              <div class="global_btn" @click="handleEditAntenna(row)" >
                <el-icon><Edit /></el-icon>
                <span class="table_btn">{{ t('antennaManage.operation.edit') }}</span>
                </div>
              </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <Dialog v-bind="antennaDiaLog" v-model:dialogVisible="antennaDiaLog.dialogVisible" >
        <template #content>
          <div  class="p-20px">
           <div v-if="antennaDiaLog.changeType === 'antenna'">
              <el-form ref="antennaFormRef" :model="antennaForm" :rules="antennaFormRules" label-width="auto" label-position="left">
                <el-form-item :label="t('antennaManage.name')" prop="name">
                  <el-input v-model="antennaForm.name" />
                </el-form-item>
                <el-form-item :label="t('antennaManage.ip_addr')" prop="ip_addr">
                  <el-input v-model="antennaForm.ip_addr" />
                </el-form-item>
                <el-form-item :label="t('antennaManage.ip_port')" prop="ip_port">
                  <el-input v-model.number="antennaForm.ip_port"  />
                </el-form-item>
                <el-form-item :label="t('antennaManage.local_port')" prop="local_port">
                  <el-input v-model.number="antennaForm.local_port"  />
                </el-form-item>
                <el-form-item :label="t('antennaManage.serial_device')" prop="serial_name">
                  <el-input v-model="antennaForm.serial_name" />
                </el-form-item>
                <el-form-item :label="t('antennaManage.cp_label')" prop="cp_device">
                  <MSelect v-bind="basebandInfoConfig" v-model="antennaForm.cp_device" v-if="basebandNum > 1" />
                  <el-input disabled v-model="antennaForm.cp_device" v-else />
                </el-form-item>
                <el-form-item :label="t('antennaManage.enable_label')" prop="enable">
                  <el-switch  v-model="antennaForm.enable" active-value="on" inactive-value="off" />
                </el-form-item>
              </el-form>
              <div class="m-t-30px dialog_bottom">
              <div class="confirm_btn" @click="handleSaveCommunication">{{t('networkConfig.save_label')}}</div>
              <div class="cancel_btn" @click="antennaDiaLog.dialogVisible = false">{{t('networkConfig.cancel_label')}}</div>
            </div>
           </div>   
           <div v-else-if="antennaDiaLog.changeType === 'param'">
             <el-form ref="paramFormRef" :model="paramForm" :rules="paramFormRules" label-width="auto" label-position="left">
                <el-form-item :label="t('antennaManage.transmit_switch_label')" prop="transmit_switch">
                  <el-switch v-model="paramForm.transmit_switch" active-value="on" inactive-value="off" />
                </el-form-item>
               
              <!--   <el-form-item :label="t('antennaManage.track_switch_label')" prop="tracking_switch">
                  <el-switch v-model="paramForm.tracking_switch" active-value="on" inactive-value="off" />
                </el-form-item> -->
                 <el-form-item :label="t('antennaManage.transmit_frequency_label')" prop="transmit_frequency">
                  <el-input v-model.number="paramForm.transmit_frequency" />
                 </el-form-item>
                 <el-form-item :label="t('antennaManage.receive_frequency_label')" prop="receive_frequency">
                  <el-input v-model.number="paramForm.receive_frequency" />
                 </el-form-item>
                 <el-form-item :label="t('antennaManage.transmit_polarization_label')" prop="transmit_polarization">
                  <MSelect v-bind="paramConfig" v-model="paramForm.transmit_polarization"/>
                 </el-form-item>
                 <el-form-item :label="t('antennaManage.receive_polarization_label')" prop="receive_polarization">
                  <MSelect v-bind="paramConfig" v-model="paramForm.receive_polarization"/>
                 </el-form-item>
                 <el-form-item :label="t('antennaManage.buc_attenuation_label')" prop="buc">
                  <el-input v-model.number="paramForm.buc" />
                 </el-form-item>
                 <el-form-item :label="t('antennaManage.bdc_attenuation_label')" prop="bdc">
                  <el-input v-model.number="paramForm.bdc" />
                 </el-form-item>
             </el-form>
             <div class="m-t-30px dialog_bottom">
              <div class="confirm_btn" @click="handleSaveBaseParam">{{t('networkConfig.save_label')}}</div>
              <div class="cancel_btn" @click="antennaDiaLog.dialogVisible = false">{{t('networkConfig.cancel_label')}}</div>
            </div>
           </div>
           <div v-else>
            <el-form 
            ref="addAntennaFormRef" 
            :model="addAntennaForm" 
            :rules="addAntennaFormRules" 
            class="status-form"
            label-position="right"
            label-width="auto"
            >
                <el-form-item :label="t('antennaManage.enable_label')" prop="communication.enable">
                  <el-switch v-model="addAntennaForm.communication.enable" active-value="on" inactive-value="off" />
                 </el-form-item>                
                 <el-form-item :label="t('antennaManage.transmit_switch_label')" prop="param.transmit_switch">
                  <el-switch v-model="addAntennaForm.param.transmit_switch" active-value="on" inactive-value="off" />
                 </el-form-item>
                 <el-form-item :label="t('antennaManage.receive_switch_label')" prop="param.receive_switch">
                  <el-switch v-model="addAntennaForm.param.receive_switch" active-value="on" inactive-value="off" />
                 </el-form-item>
                 <el-form-item :label="t('antennaManage.track_switch_label')" prop="param.track_switch">
                  <el-switch v-model="addAntennaForm.param.track_switch" active-value="on" inactive-value="off" />
                 </el-form-item>  
                <el-form-item :label="t('antennaManage.name')" prop="communication.name">
                  <el-input v-model="addAntennaForm.communication.name" />
                </el-form-item>
                <el-form-item :label="t('antennaManage.ip_addr')" prop="communication.ip_addr">
                  <el-input v-model="addAntennaForm.communication.ip_addr" />
                </el-form-item>
                <el-form-item :label="t('antennaManage.ant_port')" prop="communication.ip_port">
                  <el-input v-model.number="addAntennaForm.communication.ip_port" />
                </el-form-item>
                <el-form-item :label="t('antennaManage.local_port')" prop="communication.local_port">
                  <el-input v-model.number="addAntennaForm.communication.local_port" />
                </el-form-item>
                <el-form-item :label="t('antennaManage.serial_device')" prop="communication.serial_name">
                  <el-input v-model="addAntennaForm.communication.serial_name" />
                </el-form-item>
                <el-form-item :label="t('antennaManage.serial_baudrate')" prop="communication.serial_baud">
                  <el-input v-model.number="addAntennaForm.communication.serial_baud" />
                </el-form-item>
                 <el-form-item :label="t('antennaManage.transmit_frequency_label')" prop="param.transmit_frequency">
                   <el-input v-model.number="addAntennaForm.param.transmit_frequency" />
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.receive_frequency_label')" prop="param.receive_frequency">
                    <el-input v-model.number="addAntennaForm.param.receive_frequency" />
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.transmit_polarization_label')" prop="param.transmit_polarization">
                    <MSelect  v-bind="paramConfig" v-model="addAntennaForm.param.transmit_polarization"/>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.receive_polarization_label')" prop="param.receive_polarization">
                    <MSelect  v-bind="paramConfig" v-model="addAntennaForm.param.receive_polarization"/>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.buc_attenuation_label')" prop="param.buc">
                    <el-input v-model="addAntennaForm.param.buc"></el-input>
                  </el-form-item>
                  <el-form-item :label="t('antennaManage.bdc_attenuation_label')" prop="param.bdc">
                    <el-input v-model="addAntennaForm.param.bdc"></el-input>
                  </el-form-item> 
            </el-form>
            <div class="dialog_bottom">
              <div class="confirm_btn" @click="handleSaveAddAntenna">{{t('networkConfig.save_label')}}</div>
              <div class="cancel_btn" @click="antennaDiaLog.dialogVisible = false">{{t('networkConfig.cancel_label')}}</div>
            </div>
           </div>
          </div>
        </template>
    </Dialog>
  </div>
</template>

<script lang="ts">
import { defineComponent } from 'vue'
import { Setting, Monitor,Edit,ArrowDown,ArrowUp} from '@element-plus/icons-vue'

export default defineComponent({
  name: 'antennaManage',
})
</script>

<script setup lang="ts">
import { antennaManageApi } from '@/api/antennaManage'
import {BasebandApi} from '@/api/basebandControl'
import { useI18n } from 'vue-i18n'
import { cloneDeep } from 'lodash'
import { ElNotificationFn } from '@/utils/computedUtils'
import { useUserInfoStore } from '@/stores/userInfo'
import { storeToRefs } from 'pinia'
const userInfoStore = useUserInfoStore()
const {basebandNum} = storeToRefs(userInfoStore)
const { t } = useI18n()
const antennaList = ref<any>([])
const postAntennaInfo = async () => {
  const { data } = await antennaManageApi.reqPostAntennaInfo()
  data.ant = data.ant.map((item:any)=>{
    return item
  })
  antennaList.value = data.ant
}
const tableRowClassName = (row: any, rowIndex: number) => {
  if (rowIndex % 2 === 0) {
    return 'odd_Rows'
  } else {
    return 'even_Rows'
  }
}
const expands = ref<string[]>([])
const currentId = ref<any>("")
function expandAllRows() {
  if (antennaList.value && antennaList.value.length > 0) {
    expands.value = antennaList.value.map((item: any) => item.id)
  }
}
const isExpanded = ref<boolean>(false)
watch(() => antennaList.value, (newVal) => {
  if (newVal && newVal.length > 0) {
    if(!isExpanded.value){
      expandAllRows() 
      isExpanded.value = true
    }
  }
}, { immediate: true })
/* 
每个行的展开状态完全取决于其ID是否存在于 expands 数组中
点击展开/折叠按钮时，直接添加或移除对应ID
*/
const expandChange = (row: any) => {
  currentId.value = row.id
  if (expands.value.includes(row.id)) {
    expands.value = expands.value.filter(id => id !== row.id)
  } else {
    expands.value.push(row.id)
  }
}

function setPolarization(row: any, type: string) {
  if (type === 'transmit') {
    return row.transmit_polarization === "right" ? t('antennaManage.right_circular_polarization_label') : t('antennaManage.left_circular_polarization_label')
  } else {
    return row.receive_polarization === "right" ? t('antennaManage.right_circular_polarization_label') : t('antennaManage.left_circular_polarization_label')
  }
}
const statusConfig: any = {
  setWorkMode: (type: string) => (type === "on" ? t('antennaManage.work_mode_1') : t('antennaManage.work_mode_2')),
  setStatus: (type: string) => (type === "normal" ? t('antennaManage.normal_status') : t('antennaManage.abnormal_status')),
}
const antennaDiaLog = ref<any>({
  title:'',
  width:'500',
  dialogVisible:false,
  top:"",
  changeType:"antenna"
})
const antennaForm = ref<any>({})
const antennaFormRef = ref<any>(null)
const antennaFormRules = computed(()=>{
  return {
    name:[{required:true,message:t('antennaManage.name'),trigger:'blur'}],
    ip_addr:[{required:true,message:t('antennaManage.ip_addr'),trigger:'blur'}],
    ip_port:[{required:true,message:t('antennaManage.ant_port'),trigger:'blur'}],
    local_port:[{required:true,message:t('antennaManage.local_port'),trigger:'blur'}],
    serial_name:[{required:true,message:t('antennaManage.serial_device'),trigger:'blur'}],
    cp_device:[{required:true,message:t('antennaManage.cp_label'),trigger:'blur'}],
    enable:[{required:true,message:t('antennaManage.enable_label'),trigger:'blur'}],
  }
})
function handleEditAntenna(row:any){
  antennaDiaLog.value.dialogVisible = true
  antennaDiaLog.value.changeType = 'antenna'
  antennaDiaLog.value.title = `${t('antennaManage.operation.edit')} ${row.communication.name}`
  antennaForm.value = {
    ...cloneDeep(row.communication),
    id:row.id
  }
}
function handleSaveCommunication(){
    antennaFormRef.value.validate( async (valid:any)=>{
       if(valid){
          try {
            await antennaManageApi.reqPostAntennaCommunication(antennaForm.value)
            ElNotificationFn('success',t('antennaManage.save_success_label'),'')
            postAntennaInfo()
          } catch (error) {
            ElNotificationFn('error',t('antennaManage.save_error_label'),'')
          }finally{
            antennaDiaLog.value.dialogVisible = false
          }
         }else{
        ElNotificationFn('error',t('antennaManage.save_error_label'),'')
       }
    })
}
const validateBucBdc = (rule:any,value:any,callback:any)=>{
   const num = Number(value)
   if(isNaN(num)){
    callback(new Error(t('antennaManage.attenuation_range_error')))
   }else if(num < 0 || num > 31){
    callback(new Error(t('antennaManage.attenuation_range_error')))
   }else{
    callback()
   }
}
const paramForm = ref<any>({})  
const paramFormRef = ref<any>(null)
const paramFormRules = computed(()=>{
  return {
    transmit_switch:[{required:true,message:t('antennaManage.transmit_switch_label'),trigger:'blur'}],
    receive_switch:[{required:true,message:t('antennaManage.receive_switch_label'),trigger:'blur'}],
    tracking_switch:[{required:true,message:t('antennaManage.track_switch_label'),trigger:'blur'}],
    transmit_frequency:[{required:true,message:t('antennaManage.transmit_frequency_label'),trigger:'blur'}],
    receive_frequency:[{required:true,message:t('antennaManage.receive_frequency_label'),trigger:'blur'}],
    transmit_polarization:[{required:true,message:t('antennaManage.transmit_polarization_label'),trigger:'blur'}],
    receive_polarization:[{required:true,message:t('antennaManage.receive_polarization_label'),trigger:'blur'}],
    buc: [
      { required: true, message: t('antennaManage.buc_attenuation_label'), trigger: 'blur' },
      { validator: validateBucBdc, trigger: 'blur' }
    ],
    bdc: [
      { required: true, message: t('antennaManage.bdc_attenuation_label'), trigger: 'blur' },
      { validator: validateBucBdc, trigger: 'blur' }
    ]
  }
})
async function handleEditParam(param:any,id:any){ 
  paramForm.value = {
    ...cloneDeep(param),
    id:id
  }
  antennaDiaLog.value.dialogVisible = true
  antennaDiaLog.value.changeType = 'param'
  antennaDiaLog.value.top="35vh"
  const name = antennaList.value.find((item:any)=>item.id === id)?.communication.name
  antennaDiaLog.value.title = `${t('antennaManage.operation.edit')} ${name} ${t('antennaManage.param_label')}`
}
const paramConfig = computed<any>(()=>{
  return {
    options:[
      {label:t('antennaManage.right_circular_polarization_label'),value:'right'},
      {label:t('antennaManage.left_circular_polarization_label'),value:'left'}
    ],
    placeholder:t('antennaManage.transmit_polarization_label'),
    width:"80%"
  }
})
function handleSaveBaseParam(){
    paramFormRef.value.validate( async (valid:any)=>{
    if(valid){
      try {
      await antennaManageApi.reqPostAntennaParam(paramForm.value)
      ElNotificationFn('success',t('antennaManage.save_success_label'),'')
       postAntennaInfo()
      }catch (error) {
        ElNotificationFn('error',t('antennaManage.save_error_label'),'')
      }finally{
        antennaDiaLog.value.dialogVisible = false
      }
     }else{
        ElNotificationFn('error',t('antennaManage.save_error_label'),'')
      }
     })
  }

function handleAddAntenna(){
  antennaDiaLog.value.dialogVisible = true
  antennaDiaLog.value.changeType = 'addAntenna'
  antennaDiaLog.value.width="60%"
  antennaDiaLog.value.title = t('antennaManage.add_antenna')
}
const addAntennaForm = ref<any>({
  param:{
    transmit_switch:"",
    receive_switch:"",
    tracking_switch:"",
    transmit_frequency:"",
    receive_frequency:"",
    transmit_polarization:"",
    receive_polarization:"",
    buc:"",
    bdc:""
  },
  communication:{
    name:"",
    ip_addr:"",
    ip_port:"",
    local_port:"",
    serial_name:"",
    serial_baud:"",
    enable:"",
    status:""
  }
})
const addAntennaFormRef = ref<any>(null)
const addAntennaFormRules = computed(()=>{
  return {
    "communication.name":[
      {required:true,message:t('antennaManage.name'),trigger:'blur'}
    ],
    "communication.ip_addr":[
      {required:true,message:t('antennaManage.ip_addr'),trigger:'blur'}
    ],
    "communication.ip_port":[
      {required:true,message:t('antennaManage.ant_port'),trigger:'blur'}
    ],
    "communication.local_port":[
      {required:true,message:t('antennaManage.local_port'),trigger:'blur'}
    ],
    "communication.serial_name":[
      {required:true,message:t('antennaManage.serial_device'),trigger:'blur'}
    ],
    "communication.serial_baud":[
      {required:true,message:t('antennaManage.serial_baudrate'),trigger:'blur'}
    ],
    "communication.enable":[
      {required:true,message:t('antennaManage.enable_label'),trigger:'blur'}
    ],
    "communication.status":[
      {required:true,message:t('antennaManage.status_label'),trigger:'blur'}
    ],
    "param.transmit_switch":[
      {required:true,message:t('antennaManage.transmit_switch_label'),trigger:'blur'}
    ],
    "param.receive_switch":[
      {required:true,message:t('antennaManage.receive_switch_label'),trigger:'blur'}
    ],
    "param.track_switch":[
      {required:true,message:t('antennaManage.track_switch_label'),trigger:'blur'}
    ],
    "param.transmit_frequency":[
      {required:true,message:t('antennaManage.transmit_polarization_label'),trigger:'blur'}
    ],
    "param.receive_frequency":[
      {required:true,message:t('antennaManage.receive_frequency_label'),trigger:'blur'}
    ],
    "param.transmit_polarization":[
      {required:true,message:t('antennaManage.transmit_polarization_label'),trigger:'change'}
    ],
    "param.receive_polarization":[
      {required:true,message:t('antennaManage.receive_polarization_label'),trigger:'change'}
    ],
    "param.buc":[
      {required:true,message:t('antennaManage.buc_attenuation_label'),trigger:'blur'}
    ],
    "param.bdc":[
      {required:true,message:t('antennaManage.bdc_attenuation_label'),trigger:'blur'}],

  }
})
async function handleSaveAddAntenna(){
  try {
    await addAntennaFormRef.value.validate()
    await antennaManageApi.reqPostAntennaSet(addAntennaForm.value)
    antennaDiaLog.value.dialogVisible = false
    ElNotificationFn('success',t('antennaManage.save_success_label'),'')
    postAntennaInfo()
  } catch (error) {
    console.log(error,"error");  
  }
}
async function handleDeleteAntenna(row:any) {
  await antennaManageApi.reqPostAntennaDelete(row)
  ElNotificationFn('success',t('antennaManage.delete_success_label'),'')
  postAntennaInfo()
}
const basebandInfoConfig = computed(():any=>{
  return {
    options:[],
    placeholder:t('antennaManage.select_baseband'),
    width:"80%"
  }
})
async function getBasebandInfo(){
  const {data} = await BasebandApi.reqGetBasebandInfo()
  basebandInfoConfig.value.options = data.info.map((item:any)=>{
    return {
      label:item.name,
      value:item.name
    }
  })
}

let antennaTimer = ref<any>(null)
const clearAntennaTimer = () => {
  if (antennaTimer.value) {
    clearInterval(antennaTimer.value)
    antennaTimer.value = null
  }
}
function scheduleNextUpdate(){
  antennaTimer.value = setTimeout(()=>{
    postAntennaInfo().finally(()=>{
      if(antennaTimer.value){
        scheduleNextUpdate()
      }
    })
  },3000)
}
onActivated(() => {
  postAntennaInfo()
  getBasebandInfo()
  scheduleNextUpdate() 
})
onDeactivated(clearAntennaTimer)
onUnmounted(clearAntennaTimer)
</script>

<style lang="scss" scoped>
@import '@/styles/table.scss';
@import '@/styles/form.scss';
@import '@/styles/pageLabel.scss';
@import '@/styles/input.scss';
@import '@/styles/card.scss';
:deep(.el-table__expand-icon) {
  display: none;
}
.el-table {
  .el-button.is-text:not(.is-disabled):hover {
    background-color: transparent !important;
  }
}
:deep(.el-icon) {
  font-size: 18px;
}
.el-button {
  font-weight: 500;
    font-size: 15px;
    color: #00D4FF;
    font-family: PingFangSC;
  .el-icon {
    margin-right: 4px;
  }
   
}
:deep(.el-button.is-text) {
  color: #00D4FF;
}
.m-r-10px {
  margin-right: 10px;
}
.m-l-5px {
  margin-left: 5px;
}
.m-b-20 {
  margin-bottom: 20px;
}

</style>
